package cn.cttic.order.service.impl;


import cn.cttic.dashboard.domain.*;
import cn.cttic.dashboard.mapper.WorkbenchMapper;
import cn.cttic.dashboard.service.IWorkbenchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 备案信息Service业务层处理
 *
 * @author Times.Studio
 * @date 2024-09-30
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class WorkbenchServiceImpl implements IWorkbenchService {

    private final WorkbenchMapper baseMapper;


    /**
     * 查询指标数据
     *
     * @return 指标数据
     */
    @Override
    public FullBenchmarkVo getFullBenchmark() {
        return baseMapper.getFullBenchmark();
    }

    /**
     * 查询销售排名
     *
     * @return 销售排名
     */
    @Override
    public List<SaleRankVo> getSaleRank() {
        return baseMapper.getSaleRank();
    }

    /**
     * 查询安装排名
     *
     * @return 安装排名
     */
    @Override
    public List<InstallRankVo> getInstallRank() {
        return baseMapper.getInstallRank();
    }

    /**
     * 查询验收排名
     *
     * @return 验收排名
     */
    @Override
    public List<VerifyRankVo> getVerifyRank() {
        return baseMapper.getVerifyRank();
    }


    /**
     * 查询产品服务统计数据
     *
     * @return 产品服务统计数据
     */
    @Override
    public List<ProductCounterStatisticVo> getProductCounterStatistic() {
        return baseMapper.getProductCounterStatistics();
    }

    /**
     * 查询产品销售年度分布数据
     *
     * @param productId
     * @return 产品销售年度分布数据
     */
    @Override
    public ProductSaleByMonthOverYearDisplayVo getProductSaleByMonthOverYear(String productId) {
        ProductSaleByMonthOverYearDisplayVo display = new ProductSaleByMonthOverYearDisplayVo();
        try {
            List<ProductSaleByMonthOverYearTableVo> table = baseMapper.getProductSaleByMonthOverYear(productId);
            display.setTable(table);
            List<ProductSaleByMonthOverYearChartVo> chart = new ArrayList<>();
            int year = LocalDate.now().getYear();
            List<String> productList = table.stream().map(ProductSaleByMonthOverYearTableVo::getProductName).collect(Collectors.toList());
            for (ProductSaleByMonthOverYearTableVo productValueMap : table) {
                for (int j = 1; j <= 12; j++) {
                    ProductSaleByMonthOverYearChartVo chartCell = new ProductSaleByMonthOverYearChartVo();
                    chartCell.setName(String.format("%d-%d", year, j));
                    chartCell.setType(productValueMap.getProductName());
                    Field field = ProductSaleByMonthOverYearTableVo.class.getDeclaredField(String.format("m%d", j));
                    field.setAccessible(true);
                    Integer value = (Integer)field.get(productValueMap);
                    chartCell.setValue(value);
                    chart.add(chartCell);
                }
            }
            display.setChart(chart);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return display;
    }

    /**
     * 查询销售安装情况分布
     *
     * @param startDate
     * @param endDate
     * @return 销售安装情况分布
     */
    @Override
    public List<SaleInstallByDayOverPeriodChartVo> getSaleInstallByDayOverPeriod(LocalDateTime startDate, LocalDateTime endDate) {
        List<SaleInstallByDayOverPeriodChartVo> display = new ArrayList<>();
        List<SaleInstallByDayOverPeriodVo> data = baseMapper.getSaleInstallByDayOverPeriod(startDate, endDate);
        for (SaleInstallByDayOverPeriodVo vo : data) {
            int year = vo.getDateVal().getYear();
            int month = vo.getDateVal().getMonthValue();
            int day = vo.getDateVal().getDayOfMonth();
            SaleInstallByDayOverPeriodChartVo saleChartCell = new SaleInstallByDayOverPeriodChartVo();
            saleChartCell.setName(String.format("%d-%d-%d", year, month, day));
            saleChartCell.setType("销售");
            saleChartCell.setValue(BigDecimal.valueOf(vo.getTotalSaleCount()));
            saleChartCell.setSeriesType("bar");
            saleChartCell.setYAxisIndex(0);
            display.add(saleChartCell);
            SaleInstallByDayOverPeriodChartVo installChartCell = new SaleInstallByDayOverPeriodChartVo();
            installChartCell.setName(String.format("%d-%d-%d", year, month, day));
            installChartCell.setType("安装");
            installChartCell.setValue(BigDecimal.valueOf(vo.getTotalInstallCount()));
            installChartCell.setSeriesType("bar");
            installChartCell.setYAxisIndex(0);
            display.add(installChartCell);
            SaleInstallByDayOverPeriodChartVo installRateChartCell = new SaleInstallByDayOverPeriodChartVo();
            installRateChartCell.setName(String.format("%d-%d-%d", year, month, day));
            installRateChartCell.setType("安装率");
            installRateChartCell.setValue(vo.getInstallRatePercentage());
            installRateChartCell.setSeriesType("line");
            installRateChartCell.setYAxisIndex(1);
            display.add(installRateChartCell);
        }
        return display;
    }

    /**
     * 查询作业流年度统计分析
     *
     * @param year
     * @return 作业流年度统计分析
     */
    @Override
    public WorkStatisticByMonthOverYearDisplayVo getWorkStatisticByMonthOverYear(Integer year) {
        WorkStatisticByMonthOverYearDisplayVo display = new WorkStatisticByMonthOverYearDisplayVo();
        try {
            List<WorkStatisticByMonthOverYearVo> data = baseMapper.getWorkStatisticByMonthOverYear(year);
            List<WorkStatisticByMonthOverYearTableVo> table = new ArrayList<>();
            WorkStatisticByMonthOverYearTableVo saleRow = new WorkStatisticByMonthOverYearTableVo();
            WorkStatisticByMonthOverYearTableVo installRow = new WorkStatisticByMonthOverYearTableVo();
            WorkStatisticByMonthOverYearTableVo activeRow = new WorkStatisticByMonthOverYearTableVo();
            WorkStatisticByMonthOverYearTableVo verifyRow = new WorkStatisticByMonthOverYearTableVo();
            WorkStatisticByMonthOverYearTableVo qcRow = new WorkStatisticByMonthOverYearTableVo();

            saleRow.setWorkName("销售"); saleRow.setTotalCount(0);
            installRow.setWorkName("安装"); installRow.setTotalCount(0);
            activeRow.setWorkName("激活"); activeRow.setTotalCount(0);
            verifyRow.setWorkName("验收"); verifyRow.setTotalCount(0);
            qcRow.setWorkName("质控"); qcRow.setTotalCount(0);
//            List<ProductSaleByMonthOverYearChartVo> chart = new ArrayList<>();
//            List<String> productList = table.stream().map(ProductSaleByMonthOverYearTableVo::getProductName).collect(Collectors.toList());

            for (int i = 1; i < 12; i++) {
                Field field = WorkStatisticByMonthOverYearTableVo.class.getDeclaredField(String.format("m%d", i));
                field.setAccessible(true);
                field.set(saleRow, 0);
                field.set(installRow, 0);
                field.set(activeRow, 0);
                field.set(verifyRow, 0);
                field.set(qcRow, 0);
            }

            for (WorkStatisticByMonthOverYearVo workStatistic : data) {
                int month = workStatistic.getMonthVal();
                Field field = WorkStatisticByMonthOverYearTableVo.class.getDeclaredField(String.format("m%d", month));
                field.setAccessible(true);
                field.set(saleRow, workStatistic.getTotalSaleCount());
                field.set(installRow, workStatistic.getTotalInstallCount());
                field.set(activeRow, workStatistic.getTotalActiveCount());
                field.set(verifyRow, workStatistic.getTotalVerifyCount());
                field.set(qcRow, workStatistic.getTotalReviewCount());
            }

            for (int j = 1; j <= 12; j++) {
                Field field = WorkStatisticByMonthOverYearTableVo.class.getDeclaredField(String.format("m%d", j));
                field.setAccessible(true);
                saleRow.setTotalCount(saleRow.getTotalCount() + (Integer) field.get(saleRow));
                installRow.setTotalCount(installRow.getTotalCount() + (Integer) field.get(installRow));
                activeRow.setTotalCount(activeRow.getTotalCount() + (Integer) field.get(activeRow));
                verifyRow.setTotalCount(verifyRow.getTotalCount() + (Integer) field.get(verifyRow));
                qcRow.setTotalCount(qcRow.getTotalCount() + (Integer) field.get(qcRow));
            }
            table.add(saleRow);
            table.add(installRow);
            table.add(activeRow);
            table.add(verifyRow);
            table.add(qcRow);
            display.setTable(table);

            List<WorkStatisticByMonthOverYearChartVo> chart = new ArrayList<>();
            for (int j = 1; j <= 12; j++) {
                Field field = WorkStatisticByMonthOverYearTableVo.class.getDeclaredField(String.format("m%d", j));
                field.setAccessible(true);
                WorkStatisticByMonthOverYearChartVo saleChartCell = new WorkStatisticByMonthOverYearChartVo();
                saleChartCell.setName(String.format("%d-%d", year, j));
                saleChartCell.setType("销售");
                Integer saleRowVal = (Integer)field.get(saleRow);
                saleChartCell.setValue(saleRowVal);
                WorkStatisticByMonthOverYearChartVo installChartCell = new WorkStatisticByMonthOverYearChartVo();
                installChartCell.setName(String.format("%d-%d", year, j));
                installChartCell.setType("安装");
                Integer installRowVal = (Integer)field.get(installRow);
                installChartCell.setValue(installRowVal);
                WorkStatisticByMonthOverYearChartVo activeChartCell = new WorkStatisticByMonthOverYearChartVo();
                activeChartCell.setName(String.format("%d-%d", year, j));
                activeChartCell.setType("激活");
                Integer activeRowVal = (Integer)field.get(activeRow);
                activeChartCell.setValue(activeRowVal);
                WorkStatisticByMonthOverYearChartVo verifyChartCell = new WorkStatisticByMonthOverYearChartVo();
                verifyChartCell.setName(String.format("%d-%d", year, j));
                verifyChartCell.setType("验收");
                Integer verifyRowVal = (Integer)field.get(verifyRow);
                verifyChartCell.setValue(verifyRowVal);
                WorkStatisticByMonthOverYearChartVo qcChartCell = new WorkStatisticByMonthOverYearChartVo();
                qcChartCell.setName(String.format("%d-%d", year, j));
                qcChartCell.setType("质控");
                Integer qcRowVal = (Integer)field.get(qcRow);
                qcChartCell.setValue(qcRowVal);
                chart.add(saleChartCell);
                chart.add(installChartCell);
                chart.add(activeChartCell);
                chart.add(verifyChartCell);
                chart.add(qcChartCell);
            }
//            for (WorkStatisticByMonthOverYearVo workStatistic : data) {
//                int month = workStatistic.getMonthVal();
//                WorkStatisticByMonthOverYearChartVo saleChartCell = new WorkStatisticByMonthOverYearChartVo();
//                saleChartCell.setName(String.format("%d-%d", year, month));
//                saleChartCell.setType("销售");
//                saleChartCell.setValue(workStatistic.getTotalSaleCount());
//                WorkStatisticByMonthOverYearChartVo installChartCell = new WorkStatisticByMonthOverYearChartVo();
//                installChartCell.setName(String.format("%d-%d", year, month));
//                installChartCell.setType("安装");
//                installChartCell.setValue(workStatistic.getTotalInstallCount());
//                WorkStatisticByMonthOverYearChartVo activeChartCell = new WorkStatisticByMonthOverYearChartVo();
//                activeChartCell.setName(String.format("%d-%d", year, month));
//                activeChartCell.setType("激活");
//                activeChartCell.setValue(workStatistic.getTotalActiveCount());
//                WorkStatisticByMonthOverYearChartVo verifyChartCell = new WorkStatisticByMonthOverYearChartVo();
//                verifyChartCell.setName(String.format("%d-%d", year, month));
//                verifyChartCell.setType("验收");
//                verifyChartCell.setValue(workStatistic.getTotalVerifyCount());
//                WorkStatisticByMonthOverYearChartVo qcChartCell = new WorkStatisticByMonthOverYearChartVo();
//                qcChartCell.setName(String.format("%d-%d", year, month));
//                qcChartCell.setType("质控");
//                qcChartCell.setValue(workStatistic.getTotalReviewCount());
//                chart.add(saleChartCell);
//                chart.add(installChartCell);
//                chart.add(activeChartCell);
//                chart.add(verifyChartCell);
//                chart.add(qcChartCell);
//            }
            display.setChart(chart);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return display;
    }
}
